package com.sq.partner.manager.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service;

import com.sq.partner.manager.dao.CommonDao;
import com.sq.partner.manager.dao.impl.BaseDaoImpl;
import com.sq.partner.manager.mybatis.model.GatewayAppTranstype;
import com.sq.partner.manager.service.IGatewayAppTransService;
import com.sq.partner.manager.util.DBUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
@Service
public class GatewayAppTransServiceImpl implements IGatewayAppTransService {
	private static Logger logger = LogUtil.getLog(BaseDaoImpl.class);
	@Resource(name="sqlSession")
	private SqlSessionTemplate session; 
	@Resource
	private CommonDao commonDao;
	
	@Override
	public List<GatewayAppTranstype> getListAppTransInfoDao(GatewayAppTranstype appVo) throws Exception{//queryGatewayAppTransByAppId
		   List<GatewayAppTranstype> list=new ArrayList<GatewayAppTranstype>();
		   list = commonDao.queryList("queryGatewayAppTransByMap", appVo);
		   return list;
	   }
	@Override
	public List<GatewayAppTranstype> getListAppTransInfo(String appID) throws Exception{//queryGatewayAppTransByAppId
		   List<GatewayAppTranstype> list=new ArrayList<GatewayAppTranstype>();
		   list = commonDao.queryList("queryGatewayAppTransByAppId", appID);
		   return list;
	   }
	
	@Override
	public Page getListToPage(String clumnNames,String appID) throws Exception{
		  return commonDao.queryPage(1, 10, clumnNames, "t_gateway_app", " 1=1 ", appID, null);
	}
	@Override
	public boolean addGatewayAppTrans(GatewayAppTranstype app) throws Exception{
		return commonDao.add("insertGatewayAppTrans", app);
	}
	@Override
	public boolean deleteGatewayAppTrans(String appTransId) throws Exception{
		return commonDao.delete("delGatewayAppTransById", appTransId);
	}
	@Override
	public boolean updateGatewayAppTrans(GatewayAppTranstype app) throws Exception{
		return commonDao.add("updateGatewayAppTrans", app);
	}
	@Override
	public Page queryPageAppTrans(Page page,String appName){
		//
		Connection conn = null;
		//查总条数
		PreparedStatement pstmt = null;
		//查分页
		PreparedStatement pstmt1 = null;
		ResultSet rs = null;
		SqlSessionTemplate st = (SqlSessionTemplate)session;
		
		try{
			
			conn = SqlSessionUtils.getSqlSession(st.getSqlSessionFactory(), st.getExecutorType(),
	                st.getPersistenceExceptionTranslator()).getConnection();
			StringBuffer sqlbuf=new StringBuffer();
			sqlbuf.append("select APP_TRANSTYPE_ID,APP_Id,TRANS_CODE,TRANS_NAME from t_gateway_app_transtype where 1=1");
			String sql;
					if(null!=appName&&appName.trim()!=""){
						sqlbuf.append(" and TRANS_NAME='"+appName+"'");
					}
			sql=sqlbuf.toString();
			logger.debug("执行SQL：" + sql);
			Long total;
			try {
				pstmt = conn.prepareStatement("select count(*) from ("+sql+")");
				rs = pstmt.executeQuery();
				rs.next();
				total = rs.getLong(1);
				
				//查出分页
				Page p = new Page(page.getCurrent(),total);
				SqlUtil sqlUtil=new	SqlUtil();
				String pagesql=sqlUtil.buildPageSqlForOracle(sql,p).toString();
				logger.debug("执行SQL："+pagesql);
				pstmt1 = conn.prepareStatement(pagesql);
				
				rs = pstmt1.executeQuery();
				List list = DBUtil.transRs2List(rs);
				p.setData(list);
				return p;
			} catch (Exception e) {
				logger.info(e);
			}
			
			
			
			
		}finally{
			if(rs!=null){
				try{
					rs.close();
				}catch (Exception e) {
					logger.error("关闭ResultSet失败",e);
				}
			}
			if(pstmt!=null){
				try{
					pstmt.close();
				}catch (Exception e) {
					logger.error("关闭PreparedStatement失败",e);
				}
			}
			if(pstmt1!=null){
				try{
					pstmt1.close();
				}catch (Exception e) {
					logger.error("关闭PreparedStatement失败",e);
				}
			}
			if(conn!=null){
				try{
					conn.close();
				}catch (Exception e) {
					logger.error("关闭conn失败",e);
				}
			}
			
		}
		return null;
	}
}
